package id.co.rho.scoring.cid.client.widgets;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.ComboBox;

public class DirtyAwareComboBox extends ComboBox<ModelData> {
	private Integer priority;
	
	Listener changeListener = new Listener<FieldEvent>() {
		public void handleEvent(FieldEvent be) {
			onFieldChange(be);
		}
	};

	/**
	 * Dirtyness previous state.
	 */
	boolean dirtyPrevState = false;
	
	public DirtyAwareComboBox() {
		super();
		addListener(Events.Change, changeListener);		
	}
	
	
	
	private void onFieldChange(FieldEvent be) {
		boolean dirty = isDirty();
		if(dirty != dirtyPrevState) {
			dirtyPrevState = dirty;
			if(dirty) {
				UIHelper.applyDirtyMarker(this);
			}else {
				UIHelper.removeDirtyMarker(this);
			}
		}
	}
	
	public void resetDirtyness() {
		dirtyPrevState = false;
		UIHelper.removeDirtyMarker(this);
		onFieldChange(null);
	}

	@Override
	public String toString() {
		return getFieldLabel();
	}

	public Integer getPriority() {
		return priority;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}
	

}
